package com.lujianfei.recyclerview.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lujianfei.recyclerview.R;
import com.lujianfei.recyclerview.entity.ListItem;

import java.util.IllegalFormatCodePointException;
import java.util.List;

public class RecyclerLoadMoreAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int TYPE_ITEM = 1;
    private final static int TYPE_LOAD_MORE = 2;
    private final static int FOOTER_COUNT = 1;

    private boolean hasMore;

    List<ListItem> listItemList;

    public RecyclerLoadMoreAdapter(List<ListItem> listItemList) {
        this.listItemList = listItemList;
    }

    public boolean isHasMore() {
        return hasMore;
    }

    public void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case TYPE_ITEM:{
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_item, parent, false);
                viewHolder = new ItemViewHolder(view);
            }
            break;
            case TYPE_LOAD_MORE:{
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.base_footer, parent, false);
                viewHolder = new FooterViewHolder(view);
            }
            break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case TYPE_ITEM: {
                ListItem listItem = this.listItemList.get(position);

                ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
                itemViewHolder.title.setText(listItem.getTitle());
                itemViewHolder.summary.setText(listItem.getSummary());
            }
            break;
            case TYPE_LOAD_MORE: {
                holder.itemView.setVisibility(hasMore ? View.VISIBLE : View.INVISIBLE);
            }
            break;
        }

    }

    @Override
    public int getItemCount() {
        return this.listItemList.size() + FOOTER_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == listItemList.size()) {
            return TYPE_LOAD_MORE;
        }
        return TYPE_ITEM;
    }

    static class FooterViewHolder extends RecyclerView.ViewHolder {

        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView title;
        TextView summary;

        public ItemViewHolder(View view) {
            super(view);
            contentView = view;
            title = view.findViewById(R.id.title);
            summary = view.findViewById(R.id.summary);
        }
    }

}
